/**
 * Author: w景洋.
 * Time: 2016/4/19 0019
 */
angular.module('entrance.service.auth', [
        "ngResource",
        "ngStorage"
    ])
    //认证服务
    .service('EntAuthService', ['$resource', '$localStorage', function ($resource, $localStorage) {
        var $storage = $localStorage.$default({
            userId: '',
            name: '',
            wxpay_openid: '',
            good_name: [],
            shopcart: {},
            authcode:''
        });
        var goods_nameList = $storage.good_name;
        //获取微信支付OpenID
        this.getOpenid = function () {
            var openid_res = $resource(ent_payconfig.wxpay_reaurl + "openid");
            if (!$storage.wxpay_openid) {
                var reg = new RegExp("(^|&)" + "code" + "=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                var authcode = "xxxxxxxxxxxxxxxxxx";
                if (r) {
                    authcode = unescape(r[2]);
                    $storage.authcode =authcode;
                    var openid = openid_res.get({
                        wxspid: ent_payconfig.wxpay_spid,
                        code: authcode
                    }).$promise.then(function (data) {
                        if (data.openid) {
                            $storage.wxpay_openid = data.openid;
                        } else {
                            console.log("can't get openid by code=" + authcode);
                        }
                    }, function (error) {
                        console.log("code is invalid");
                        });
                    return openid;
                } else {
                    console.log("can't find code");
                    return $storage.wxpay_openid;
                }
            } else {
                return $storage.wxpay_openid;
            }
        };
        //获取用户id 为空则让他去登录
        this.getUserid = function () {
            return $storage.userId;
        };
        //获取缓存中搜索名称
        this.getGood_name = function () {
            return $storage.good_name;
        }
        //清空缓存中搜索名称
        this.removeGood_name = function () {
            $storage.good_name = [];//清空本地缓存
            goods_nameList = $storage.good_name;//goods_nameList清空
        }
        //首页搜索功能
        this.searchGoods = function (search, succ) {
            var j = 0;
            //遍历历史搜索数组没有重复搜索名字才允许存入缓存数组中
            for (var i = 0; i < goods_nameList.length; i++) {
                if (search.q == goods_nameList[i]) {
                    j = 1;
                    break;
                }
            }
            if (j != 1) {
                if (search.q != '' && goods_nameList.length == 6) {
                    goods_nameList.splice(5, 1);
                    goods_nameList.unshift(search.q);
                    $storage.good_name = goods_nameList;
                } else if (search.q != '') {
                    goods_nameList.unshift(search.q);
                    $storage.good_name = goods_nameList;
                }
            }
            var searchGood = $resource(mallurl + "goodlist/bysearch")
            return searchGood.query(search, succ)
        }
    }])